#!/bin/bash

echo -e "Catch the duckies! When a duck appears, press ENTER as fast as possible.
Aim for a low average time to capture. Ctrl+C to quit.\n"
total_time=0
total_duckies=0

if [[ -n ${1} ]]; then interval="${1}"; else interval=90; fi

while true; do
	sleep $((RANDOM%interval+1))
	while read -t 1 discard; do : ; done
	duck_start=$(date +%s)
	echo "  -._.-._.-. \_o< --QUACK!"
	read
	duck_time=$(($(date +%s)-duck_start))
	((total_duckies+=1))
	((total_time+=duck_time))
	# This is a bit of a neat trick. Bash only does integer arithmetics. Therefore we first get the number of seconds in the average, and then the extant number of hundredths of seconds, and print that as a faux floating point number.
	echo -e "Yay you caught it in ${duck_time} seconds! You've caught ${total_duckies} ducks at an average time of $((total_time/total_duckies)).$((100*total_time/total_duckies%100)) seconds.\n"
done
